package com.ruoyi.project.mahjong.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.project.mahjong.domain.TMember;
import com.ruoyi.project.mahjong.domain.TTablenumber;
import com.ruoyi.project.mahjong.service.ITMemberService;
import com.ruoyi.project.mahjong.service.ITTablenumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.TOutSendMapper;
import com.ruoyi.project.mahjong.domain.TOutSend;
import com.ruoyi.project.mahjong.service.ITOutSendService;

/**
 * 断电记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-19
 */
@Service
public class TOutSendServiceImpl implements ITOutSendService
{
    @Autowired
    private TOutSendMapper tOutSendMapper;
    @Autowired
    private ITMemberService memberService;
    @Autowired
    private ITTablenumberService tablenumberService;

    /**
     * 查询断电记录
     *
     * @param id 断电记录ID
     * @return 断电记录
     */
    @Override
    public TOutSend selectTOutSendById(Long id)
    {
        return tOutSendMapper.selectTOutSendById(id);
    }

    /**
     * 查询断电记录列表
     *
     * @param tOutSend 断电记录
     * @return 断电记录
     */
    @Override
    public List<TOutSend> selectTOutSendList(TOutSend tOutSend)
    {
        return tOutSendMapper.selectTOutSendList(tOutSend);
    }

    /**
     * 新增断电记录
     *
     * @param tOutSend 断电记录
     * @return 结果
     */
    @Override
    public int insertTOutSend(TOutSend tOutSend)
    {
        return tOutSendMapper.insertTOutSend(tOutSend);
    }

    /**
     * 修改断电记录
     *
     * @param tOutSend 断电记录
     * @return 结果
     */
    @Override
    public int updateTOutSend(TOutSend tOutSend)
    {
        return tOutSendMapper.updateTOutSend(tOutSend);
    }

    /**
     * 批量删除断电记录
     *
     * @param ids 需要删除的断电记录ID
     * @return 结果
     */
    @Override
    public int deleteTOutSendByIds(Long[] ids)
    {
        return tOutSendMapper.deleteTOutSendByIds(ids);
    }

    /**
     * 删除断电记录信息
     *
     * @param id 断电记录ID
     * @return 结果
     */
    @Override
    public int deleteTOutSendById(Long id)
    {
        return tOutSendMapper.deleteTOutSendById(id);
    }

    @Override
    public List<TOutSend> selectTOutSendAndMemberNameList(TOutSend tOutSend) {
         List<TOutSend> tOutSends = tOutSendMapper.selectTOutSendAndMemberNameList(tOutSend);
       if (ObjectUtil.isNotEmpty(tOutSends)){
           {
               for (TOutSend outSend : tOutSends) {
                   if (outSend.getBak1().equals("1")) {
                       outSend.setBak1("开桌超时");
                   }
                   if (outSend.getBak1().equals("2")) {
                       outSend.setBak1("会员余额不足");
                   }
               }
           }}
         List<TMember> tMembers = memberService.selectTMemberByShopId(Long.valueOf(tOutSend.getShopId()));
           if (ObjectUtil.isNotEmpty(tMembers)){
        for (TOutSend outSend : tOutSends) {
             String memberId = outSend.getBak2();
            for (TMember tMember : tMembers) {
                if (memberId.equals(tMember.getId().toString())){
                    outSend.setName(tMember.getName());

                }
            }
        }}
        List<TTablenumber> tTablenumbers = tablenumberService.selectAllTableListByShopId(Long.valueOf(tOutSend.getShopId()));
           if (ObjectUtil.isNotEmpty(tTablenumbers)) {
               for (TOutSend outSend : tOutSends) {
                   for (TTablenumber tTablenumber : tTablenumbers) {
                       if (tTablenumber.getId().toString().equals(outSend.getTableId())) {
                           outSend.setTableName(tTablenumber.getTablenumberId());
                       }
                   }
               }
           }
           if (ObjectUtil.isNotNull(tOutSend.getTableName())){
                List<TOutSend> collect = tOutSends.stream().filter(outSend -> outSend.getTableName().equals(tOutSend.getTableName())).collect(Collectors.toList());
           return collect;
           }
        return tOutSends;
    }
}
